package jjn.round1;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jiang Jining
 * @since 2023-04-23 23:35
 */
public class LeetCode6_ZigzagConversion {
    public String convert(String s, int numRows) {
        if (numRows < 2) {
            return s;
        }
        List<StringBuilder> list = new ArrayList<>(numRows + 1);
        for (int i = 0; i < numRows; i++) {
            list.add(new StringBuilder());
        }
        int currentRow = 0, flag = -1;
        for (int i = 0; i < s.length(); i++) {
            StringBuilder stringBuilder = list.get(currentRow);
            stringBuilder.append(s.charAt(i));
            
            if (currentRow == numRows - 1 || currentRow == 0) {
                flag = -flag;
            }
            currentRow += flag;
            
        }
        StringBuilder result = new StringBuilder();
        for (StringBuilder stringBuilder : list) {
            result.append(stringBuilder);
        }
        return result.toString();
    }
}
